<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>书籍</el-breadcrumb-item>
        <el-breadcrumb-item>新用户每日阅读统计</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
      >
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-primary"
            type="text"
            size="small"
            @click.stop="info(scope)"
            >查看章节详情</el-button
          >
        </template>
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="handleExcel()"
            >导出Excel</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
    <export-message :dialogVisible="dialogVisible" ></export-message>
  </div>
</template>


<script>
import ExportMessage from "@/components/export/message";
import { common } from "@/api/cat/httpRequest";
import { appStatisticsBook as baseURL,bookCategory } from "@/api/cat/baseUrls";
export default {
  components: { ExportMessage },
  data() {
    return {
      dialogVisible: false,
      loading:false,
      page: {
        total: 0,
        pageSize: 10
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        //数据类型：1.全站每日阅读，2.全站累计阅读，3.新用户每日阅读，4.付费用户每日阅读
        data_type: 3
      },
      option: {
        index: true,
        indexLabel: "序号",
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        border: true,
        menu: true,
        column: [
          {
            label: "日期",
            prop: "statistics_date_search",
            type: "daterange",
            search: true,
            hide:true
          },
          {
            label: "日期",
            prop: "create_time",
            type: "date",
            format:"yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: false,
          },
          {
            label: "客户端",
            prop: "source",
            type: "select",
            search: true,
            dicData: [
              {
                label: "安卓",
                value: 0,
              },
              {
                label: "IOS",
                value: 1,
              },
              {
                label: "pc",
                value: 2,
              },
              {
                label: "m端",
                value: 3,
              },
              {
                label: "快应用",
                value: 4,
              },
              {
                label: "微信小程序",
                value: 5,
              },
              {
                label: "QQ小程序",
                value: 6,
              }
            ]
          },
          {
            label: "图书ID",
            prop: "book_id",
            type: "input",
            search:true  
          },
          {
            label: "图书名称",
            prop: "book_name",
            type: "input",
            search:true
          },
          {
            label: "分类",
            prop: "category_id_one",
            type: "select",
            dicData: [],
            props: {
              label: "label",
              value: "value",
            }
          },
          {
            label: "阅读UV",
            prop: "read_uv",
            type: "input",
          },
          {
            label: "扉页UV",
            prop: "read_title_page_uv",
            type: "input",
          },
          {
            label: "阅读章节数",
            prop: "read_chapter_count",
            type: "input",
            width: '110px'
          },
          {
            label: "人均阅读章节数",
            prop: "avg_read_chapter_count",
            type: "input",
            width: '110px'
          },
          {
            label: "排序",
            prop: "order_by_type",
            type: "select",
            hide: true,
            search: true,
            dicData: [
              {
                label: "阅读uv降序",
                value: 1,
              },
              {
                label: "阅读uv升序",
                value: 2,
              },
              {
                label: "人均阅读章节数降序",
                value: 3,
              },
              {
                label: "人均阅读章节数升序",
                value: 4,
              },
              {
                label: "扉页uv降序",
                value: 5,
              },
              {
                label: "扉页uv升序",
                value: 6,
              },
              {
                label: "阅读章节数降序",
                value: 7,
              },
              {
                label: "阅读章节数升序",
                value: 8,
              }
            ],
          },
        ],
      },
    };
  },
  created() {
    this.getCategoryList();
  },
  methods: {
    /**获取分类列表 */
    async getCategoryList(){
      let data = await  common(`${bookCategory.getListByLeave}/1`)
      if(this.isSuccess(data.code)){
        this.option.column[5].dicData = data.data;
      }
    },
    /**数据导出 */
    async handleExcel() {
      this.loading = true;
      this.dialogVisible = true;
      //处理搜索条件
      let params = {...this.$refs.crud.searchForm};
      params.route = "数据/书籍/新用户每日阅读统计";
      
      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000;
      if(params.statistics_date_search.length>0){
        export_condition += " &时间区间："+params.statistics_date_search[0].toLocaleDateString()+"~"+params.statistics_date_search[1].toLocaleDateString()
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }
      if(params.source){
        export_condition += " &客户端："+this.option.column[2].dicData.filter(source => source.value == params.source)[0].label;
      }
      if(params.book_id){
        export_condition += " &图书ID："+params.book_id;
      }
      if(params.book_name){
        export_condition += " &图书名称："+params.book_name;
      }
      if(params.order_by_type){
        export_condition += " &排序方式："+this.option.column[10].dicData.filter(source => source.value == params.order_by_type)[0].label;
      }

      params.export_condition = export_condition == "" ? "所有" : export_condition;
      
      //调用导出接口
      let response = await common(
        baseURL.exportToBookReadData,
        { ...params, ...this.default },
        null,
        "POST"
      );
      this.loading = false;
    },

    /**获取列表 */
    async getList() {
      this.loading = true;
      this.dialogVisible = false;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getBookReadDataList,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    } 
    
    /**刷新 */,
    refreshChange() {
      this.getList();
    },

    /**搜索 */
    searchChange(params, done) {
      if(params.statistics_date_search){
        params.startTime = params.statistics_date_search[0];
        params.endTime = params.statistics_date_search[1];
      }

      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },

    /**跳转书籍详情路由 */
    info(scope) {
      this.$router.push("/newuserread/info?book_id="+scope.row.book_id+"&source="+scope.row.source)
    },

    /**请求是否成功 */
    isSuccess(code){
      return code == 1;
    }
  },
};
</script>